﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DalatCooking.Core;
using DalatCooking.Core.Helper;
public partial class admin_Users_Profile :System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected override void OnInit(EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadProfileInfo();

            LoadRoles();
        }

        base.OnInit(e);
    }
    void LoadRoles()
    {
        foreach (var item in RoleManager.FindAll())
        {
            ddlRoles.Items.Add(new ListItem(item.RoleName, item.RoleId.ToString()));
        }

        RolesRepeater.DataSource = PersonManager.FindAllRoleById(PersonID);// user.GetRoles();
        RolesRepeater.DataBind();
    }
    public int PersonID
    {
        get
        {
            if (Server.HtmlEncode(Request.QueryString["PersonID"]) != null)
                return ConvertObject.ToInt(Server.HtmlEncode(Request.QueryString["PersonID"]));
            return Session.GetPersonLogged().PersonId;
        }
    }
    public void LoadProfileInfo()
    {
        Person p = PersonManager.FindById(PersonID);
        if (p != null)
        {
            lblProfile.Text = p.Profile.DisplayName;
            txtDispalayName.Text = p.Profile.DisplayName;
            txtFirstName.Text = p.Profile.FirstName;
            txtLastName.Text = p.Profile.LastName;
            txtMiddleName.Text = p.Profile.MiddleName;
            txtBirthday.Text = p.Profile.DateOfBirth.ToShortDateString();
            txtEmail.Text = p.Email;
            txtPhotoURL.Text = p.Profile.Avatar;
            chkPrivate.Checked = p.Profile.IsPrivate;
            txtBiography.Text = p.Profile.AboutMe;
            Address ad = AddressManager.FindById(p.Profile.AddressId);
            txtMobile.Text = ad.PhoneNumber;
            txtMainPhone.Text = ad.PhoneNumber2;
            txtFax.Text = ad.Fax;
            txtCity.Text = ad.City;
            txtState.Text = ad.State;
           
            txtCountry.Text = ad.Country;
         
            txtAddress.Text =ad.Address1;
            
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Profile pr = new DalatCooking.Core.Profile();
        pr.PersonID = PersonID;
        pr.FirstName = txtFirstName.Text;
        pr.LastName = txtLastName.Text;
        pr.DisplayName = txtDispalayName.Text;
        pr.DateOfBirth = ConvertObject.ToDateTime(txtBirthday.Text);
        pr.Avatar = txtPhotoURL.Text;
        pr.AboutMe = txtBiography.Text;
        pr.Email = txtEmail.Text;
        pr.AddressId = PersonManager.FindById(PersonID).Profile.AddressId;
        pr.IsPrivate = false;
        pr.MiddleName = txtMiddleName.Text;
        if (ProfileManager.Update(pr))
            Response.Redirect(Request.RawUrl);
    }
    protected void btnChangePass_Click(object sender, EventArgs e)
    {
        Person p = Session.GetPersonLogged();
        p.Password = txtPassword.Text.Trim();
        p.PasswordNew = txtPasswordNew2.Text.Trim();

        if (PersonManager.ChangePassword(p))
            Response.Redirect(Request.RawUrl);
    }

}